home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / DJSRC111.ZIP / go32 / bin2byte.c < prev    next >
C/C++ Source or Header  |  1993-06-26  |  1KB  |  47 lines

  1. /* This is file BIN2BYTE.C */
  2. /*
  3. ** Copyright (C) 1993 DJ Delorie, 24 Kirsten Ave, Rochester NH 03867-2954
  4. **
  5. ** This file is distributed under the terms listed in the document
  6. ** "copying.dj", available from DJ Delorie at the address above.
  7. ** A copy of "copying.dj" should accompany this file; if not, a copy
  8. ** should be available from where this file was obtained.  This file
  9. ** may not be distributed without a verbatim copy of "copying.dj".
  10. **
  11. ** This file is distributed WITHOUT ANY WARRANTY; without even the implied
  12. ** warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <fcntl.h>
  17. #include <sys/stat.h>
  18.  
  19. main(int argc, char **argv)
  20. {
  21.   int f = open(argv[1], O_RDONLY|O_BINARY);
  22.   FILE *of = fopen(argv[3], "w");
  23.   unsigned char buf[4096];
  24.   int rbytes;
  25.   int col=0, i;
  26.   if (argc < 4)
  27.   {
  28.     printf("usage: bin2byte infile symname outfile\n");
  29.     exit(1);
  30.   }
  31.   fprintf(of, "unsigned char %s[] = {\n", argv[2]);
  32.   while ((rbytes = read(f, buf, 4096)) > 0)
  33.   {
  34.     for (i=0; i<rbytes; i++)
  35.     {
  36.       fprintf(of, "%d,", buf[i]);
  37.       if (col++ == 32)
  38.       {
  39.         fprintf(of, "\n");
  40.         col = 0;
  41.       }
  42.     }
  43.   }
  44.   fprintf(of, "};\n");
  45.   fclose(of);
  46. }
  47.